<template>
    <div>
        <form action="" method="post">
            账户：<input type="text" v-model="user.loginname"><br>
            密码：<input type="password" v-model="user.loginpwd"> <br>
            <button type="button" @click="login">登  录</button>
        </form>
    </div>
</template>

<script>
import { mapMutations } from "vuex";

export default {
    name: 'LoginView',
    data() {
        return {
            user: {
                loginname: 'admin',
                loginpwd: '123'
            }
        }
    },
    methods: {
        ...mapMutations(['setUser']),
        login() {
            //api 请求接口登录

            this.axios({
                method: 'get',
                url: 'Sign',
                params: this.user
            }).then(res => {
                console.log('res: ', res);
                if (res.code == 1) {
                    //将登录成功后 返回的数据保存到 vuex 
                    this.setUser(res.data);
                    this.$message({
                        message:res.msg,
                        type:'success'
                    })
                    //2.跳转页面
                    this.$router.push('/index');
                }else{
                    alert(res.msg);
                }

            }).catch(err => {

            })

            // if (this.user.loginName == "admin" && this.user.loginPwd == "123") {
            //     //AJAX
            //     //1. 保存登录凭证
            //     let user = {
            //         UserID: 1,
            //         UserName: 'Admin',
            //         Token: 'xxxxyyyyy'
            //     }
            //     this.setUser(user)

            //     //2.跳转页面
            //     this.$router.push('/index');
            // } else {
            //     alert('账户密码错误');
            // }
        }
    },

}
</script>

<style scoped></style>